package com.teatrace.service.produce.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.teatrace.dao.BaseDao;
import com.teatrace.dao.template.DaoCallBack;
import com.teatrace.dao.template.DaoTemplate;
import com.teatrace.dao.template.impl.AutoTransactionDaoTemplate;
import com.teatrace.service.produce.ProduceService;

public class ProduceServiceImpl implements ProduceService {

	private DaoTemplate getAutoTransactionDaoTemplate() {
		return new AutoTransactionDaoTemplate();
	}

	/**
	 * 列出空地块
	 * 
	 */
	@SuppressWarnings("unchecked")
	public <T> List<T> findBlankFieldsByCriteria(final Class<T> clazz,
			final int page) throws Exception {
		return (List<T>) this.getAutoTransactionDaoTemplate().execute(
				new DaoCallBack() {
					public Object doInDao(BaseDao dao) throws Exception {
						// TODO Auto-generated method stub
						DetachedCriteria query = DetachedCriteria.forClass(
								clazz).add(Restrictions.eq("free", false));
						List list = dao.listByCriteria(query, (page - 1) * 10,
								10);// 10代表每页显示的条数，这里采用硬编码，可从配置文件读取
						return list;
					}
				});
	}


	/**
	 * 添加一个实体
	 */
	public void add(final Object object) throws Exception {
		// TODO Auto-generated method stub
		this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				dao.save(object);
				return null;
			}
		});
	}

	/**
	 * 得到一个实体
	 */
	public <T> Object loadById(final Class<T> clazz, final Serializable pk)
			throws Exception {
		return this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				Object obj = dao.load(clazz, pk);
				return obj;
			}
		});
	}

	/**
	 * 更新一个实体
	 */
	public void update(final Object obj) throws Exception {
		// TODO Auto-generated method stub
		this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				dao.saveOrUpdate(obj);
				return null;
			}
		});
	}

	/**
	 * 分页查询
	 */
	@SuppressWarnings("unchecked")
	public <T> List<T> listByPage(final Class<T> clazz, final int page)
			throws Exception {
		// TODO Auto-generated method stub
		return (List<T>) this.getAutoTransactionDaoTemplate().execute(
				new DaoCallBack() {

					public Object doInDao(BaseDao dao) throws Exception {
						// TODO Auto-generated method stub
						List list = dao.listByPage(clazz, (page - 1) * 10, 10);
						return list;
					}
				});
	}

	/**
	 * 条件分页查询
	 */
	@SuppressWarnings("unchecked")
	public <T> List<T> listByCriteria(final Class<T> clazz, final int page,final String parameter ,final Object value)
			throws Exception {
		return  (List<T>) this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				
				DetachedCriteria query = DetachedCriteria.forClass(
						clazz).add(Restrictions.eq(parameter, value));
				List list = dao.listByCriteria(query, (page - 1) * 10, 10);
				return list;
			}
		});
	
	}
	
	/**
	 * 删除一个实体
	 * @throws Exception 
	 */


	public void deleteById(final Class<?> clazz, final Serializable pk) throws Exception {
		this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			public Object doInDao(BaseDao dao) throws Exception {
				dao.delete(dao.load(clazz, pk));
				return null;
			}
		});
	}

	public <T> Object getById(final Class<T> clazz, final Serializable pk) throws Exception {
		return this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			
			public Object doInDao(BaseDao dao) throws Exception {
				Object obj = dao.get(clazz, pk);
				return obj;
			}
		});
	}

}
